//
//  MusicFMScrollView.m
//  myMusicApp
//
//  Created by 11_wyq on 15/6/16.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MusicFMScrollView.h"

@implementation MusicFMScrollView


-(instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews:array];
    }
    return self;
}

-(void)createSubviews:(NSMutableArray *)array
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 180 * HEIGHT)];
    [self addSubview:self.scrollView];
    // [self.scrollView release];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    
    
    
    
    
    NSMutableArray *arrayName = [NSMutableArray array];
    for (MusicFMModel *myMovie in array) {
        [arrayName addObject:myMovie.pic];
    }
    
    MusicFMModel *first = [array firstObject];
    [arrayName addObject:first.pic];
    MusicFMModel *last = [array lastObject];
    [arrayName insertObject:last.pic atIndex:0];
    
    
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width *arrayName.count, 0);
    
    int i = 0;
    
    for (NSString *name in arrayName) {
        
        UIImageView *im = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        im.backgroundColor = [UIColor clearColor];
        im.tag = 10000 + i;
        NSURL *url = [NSURL URLWithString:name];
        [im sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cell_bg_noData_1@2x.png"]];
        
        UIView *view = [[UIView alloc]initWithFrame:im.bounds];
        view.backgroundColor = [UIColor blackColor];
        [im addSubview:view];
        //渐变效果
        view.layer.shadowColor = [[UIColor blackColor]CGColor];
        view.alpha = 1;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1.5];//时间
        view.alpha = 0.1;
        [UIView commitAnimations];
        [view release];
        
        
        
        [self.scrollView addSubview:im];
        self.scrollView.pagingEnabled= YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [im addGestureRecognizer: tap];
        im.userInteractionEnabled = YES;
        
        i++;
    }
    
    
    
    [self.scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    [self createTimer];
    
}
#pragma mark --创建定时器
-(void)createTimer
{
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAciton:) userInfo:nil repeats:YES];
}
#pragma mark --定时器调用的方法
- (void)timerAciton:(NSTimer *)timer{
    
    //当正常滚动的时候
    self.page ++;
    //将要到达偏移量的宽度
    CGFloat offWith = self.scrollView.frame.size.width *self.page;
    //  NSLog(@"with = %f",offWith);
    
    //    [UIView animateWithDuration:0.5 animations:^{ //用动画也能实现,动画的偏移
    [self.scrollView setContentOffset:CGPointMake(offWith, 0) animated:YES];
    //  NSLog(@"正常滚动");
    //    }];
    
    
    //当不是正常滚动,滚动到边缘就取消动画
    NSInteger number = self.scrollView.contentSize.width / self.scrollView.frame.size.width;
    //number是当前图片个数
    if (offWith == self.scrollView.frame.size.width * (number - 1)) {
        self.page = 1;
        [self.scrollView setContentOffset:CGPointMake(0, 0)];
        //   NSLog(@"快速跳转");
    }
    
}

#pragma mark-- 停止定时器
-(void)stopTimer
{
    if (self.myTimer) {
        
        if (self.myTimer.isValid) {//如果是开启状态
            
            [self.myTimer invalidate];
            self.myTimer = nil;
        }
        
    }
    
}

#pragma mark--手指将要拖拽的时候停止计时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
    //  NSLog(@"timer = %@",self.myTimer);
}
#pragma mark--将要结束拖拽,也就是手指离开的时候
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    [self createTimer];
    
}

#pragma mark --加速结束的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    CGSize  size = scrollView.contentSize;
    CGFloat with = scrollView.frame.size.width;
    self.page = scrollView.contentOffset.x / with;
    
    //往左划,如果是滑到最后一张图
    if (scrollView.contentOffset.x == (size.width - with)) {
        
        self.page = 1;
        [scrollView setContentOffset:CGPointMake(with, 0)];
        
    }
    //往右滑,如果滑到第一张图
    if (scrollView.contentOffset.x == 0){
        
        self.page = scrollView.contentSize.width / scrollView.frame.size.width - 2 ;
        [scrollView setContentOffset:CGPointMake(size.width - with * 2, 0)];
    }
    
    
}


#pragma mark tapAction;
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
      NSLog(@"page = %ld",self.page);
    
    //获得点击的视图
    UIImageView *image = (UIImageView *)tap.view;
    NSLog(@"image = %ld",image.tag);
    NSInteger number = image.tag - 10000;

    
    
    
    
    return;
    
}











@end
